package com.uit.smarttool.activity;

import android.app.AlertDialog;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TabHost;

import com.google.zxing.client.android.CaptureActivity;
import com.google.zxing.client.android.HelpActivity;
import com.google.zxing.client.android.PreferencesActivity;
import com.google.zxing.client.android.history.HistoryActivity;

public final class MainActivity extends TabActivity {

	protected static final int REQUEST_SCAN = 0;

	private static final int HISTORY_ID = Menu.FIRST;
	private static final int SETTINGS_ID = Menu.FIRST + 1;	
	private static final int CHANGEPASS_ID = Menu.FIRST + 2;
	private static final int HELP_ID = Menu.FIRST + 3;	
	private static final int ABOUT_ID = Menu.FIRST + 4;

	public static final int HISTORY_REQUEST_CODE = 0x0000bacc;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		this.setContentView(R.layout.main);

		Resources res = getResources(); // Resource object to get Drawables
		TabHost tabHost = getTabHost(); // The activity TabHost
		TabHost.TabSpec spec; // Resusable TabSpec for each tab
		Intent intent; // Reusable Intent for each tab

		// Create an Intent to launch an Activity for the tab (to be reused)
		intent = new Intent().setClass(this, Tab1Activity.class);

		// Initialize a TabSpec for each tab and add it to the TabHost
		spec = tabHost.newTabSpec("shopcart")
				.setIndicator("Giỏ hàng", res.getDrawable(R.drawable.cart_icon))
				.setContent(intent);
		tabHost.addTab(spec);

		// Do the same for the other tabs
		intent = new Intent().setClass(this, Tab2Activity.class);
		spec = tabHost
				.newTabSpec("product")
				.setIndicator("Sản phẩm",
						res.getDrawable(R.drawable.product_icon))
				.setContent(intent);
		tabHost.addTab(spec);

		intent = new Intent().setClass(this, Tab3Activity.class);
		spec = tabHost
				.newTabSpec("history")
				.setIndicator("Thông tin",
						res.getDrawable(R.drawable.history_icon))
				.setContent(intent);
		tabHost.addTab(spec);

		tabHost.setCurrentTab(0);

		Button scan = (Button) findViewById(R.id.button_scan);
		Button logout = (Button) findViewById(R.id.button_logout);

		scan.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent myIntent = new Intent(v.getContext(),
						CaptureActivity.class);
				startActivityForResult(myIntent, 0);

			}
		});

		logout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
				Intent myIntent = new Intent(v.getContext(),
						LoginActivity.class);
				startActivityForResult(myIntent, 0);

			}
		});

		

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add(Menu.NONE, HISTORY_ID, Menu.NONE, R.string.menu_history)
				.setIcon(android.R.drawable.ic_menu_recent_history);
		menu.add(Menu.NONE, SETTINGS_ID, Menu.NONE, R.string.menu_settings)
				.setIcon(android.R.drawable.ic_menu_preferences);
		menu.add(Menu.NONE, CHANGEPASS_ID, Menu.NONE, R.string.menu_changepass).setIcon(
				android.R.drawable.ic_menu_add);
		menu.add(Menu.NONE, HELP_ID, Menu.NONE, R.string.menu_help).setIcon(
				android.R.drawable.ic_menu_help);
		menu.add(Menu.NONE, ABOUT_ID, Menu.NONE, R.string.menu_about).setIcon(
				android.R.drawable.ic_menu_info_details);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		Intent intent = new Intent(Intent.ACTION_VIEW);
		intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
		switch (item.getItemId()) {
		case HISTORY_ID:
			intent = new Intent(Intent.ACTION_VIEW);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
            intent.setClassName(this, HistoryActivity.class.getName());
            startActivityForResult(intent, HISTORY_REQUEST_CODE);
            break;

		case SETTINGS_ID:
			intent.setClassName(this, PreferencesActivity.class.getName());
			startActivity(intent);
			break;
		case CHANGEPASS_ID:
			intent.setClassName(this, ChangePassActivity.class.getName());
			startActivity(intent);
			break;
		case HELP_ID:
			intent.setClassName(this, HelpActivity.class.getName());
			startActivity(intent);
			break;		
		case ABOUT_ID:
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setTitle(getString(R.string.title_about));
			builder.setMessage("Nhóm:\nLê Đặng Thanh Triều 08520423 \nHoàng Đình Nam 08520241");
			builder.setIcon(R.drawable.launcher_icon);
			builder.setNegativeButton(R.string.button_cancel, null);
			builder.show();
			break;
		default:
			return super.onOptionsItemSelected(item);
		}
		return true;
	}
}
